package application;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;
import javax.naming.NamingException;

import jndi.JndiFactory;

import models.Camera;
import models.Picture;

import org.apache.log4j.Logger;
import org.imgscalr.Scalr;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import dao.CameraDao;
import dao.DaoFactory;
import dao.PictureDao;

public class PictureJob implements Job {

	final CameraDao cameraDao = DaoFactory.getInstance().getCameraDao();
	final PictureDao pictureDao = DaoFactory.getInstance().getPictureDao();
	
	private static Logger jLog = Logger.getLogger(PictureJob.class);

	/**
	 * Quartz requires a public empty constructor so that the scheduler can
	 * instantiate the class whenever it needs.
	 */
	public PictureJob() {
	}

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		System.out.println("Job gestartet");

		List<Camera> cameras = cameraDao.listAll();

		Long camId;
		String camLocation = null;
		String camURL = null;
		String picturePath = null;
		File pictureFile = null;
		File scldPictureFile = null;
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		Date now = new Date();
		String subDirectory = sdf.format(now);
		BufferedImage image = null;

		try {

			picturePath = JndiFactory.getEnvironmentAsString("picturePath");

			for (Camera cam : cameras) {

				camId = cam.getId();
				camLocation = cam.getLocation();
				camURL = cam.getUrl();

				pictureFile = new File(picturePath + File.separator
						+ camLocation + File.separator + subDirectory
						+ File.separator + now.getTime() + ".jpg");

				if (!pictureFile.exists())
					pictureFile.mkdirs();

				scldPictureFile = new File(picturePath + File.separator
						+ camLocation + File.separator + subDirectory
						+ File.separator + now.getTime() + "_scld.jpg");

				if (!scldPictureFile.exists())
					scldPictureFile.mkdirs();

				image = ImageIO.read(new URL(camURL));

				ImageIO.write(image, "jpg", pictureFile);

				ImageIO.write(Scalr.resize(image, Scalr.Method.QUALITY,
						Scalr.Mode.FIT_TO_WIDTH, 100), "jpg", scldPictureFile);
				
				Picture picture = new Picture();
				picture.setCamid(camId);
				picture.setPicpath(pictureFile.getAbsolutePath());
				picture.setPicthumbpath(scldPictureFile.getAbsolutePath());
				picture.setPictimestamp(new Timestamp(now.getTime()));
				
				if(pictureDao.save(picture)) {
					jLog.info("Bild von " + cam.getLocation() + " in DB gespeichert");
				}
				else {
					jLog.info("Bild konnte nicht in der DB gespeichert werden");
				}

			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
